var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {
    orderId: 0,
    orderDtl: {},
    services: {},
    loading: true,
    storetype: '',
    distance:0,
    orderId:null
  },
  /**
   * 页面初始化
   */
  onLoad(options) {
    this._options = options;
    this.setData({
      orderId: this._options.orderId
    })
    this.getOrderDtl();
  },
  /**
   * 获取订单详情
   */
  getOrderDtl() {
    var servicesArr = this.data.servicesArr;
    var paramsUrl = '/public/api/orders/orderdetails';
    var paramsData = {
      id: this.data.orderId,
      uid: wx.getStorageSync('uid')
    }
    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res, isLoadcb) => {
        var orderDtl = res.data;
        var services = orderDtl.services ? orderDtl.services :'';
        if(services.psfee){
          services.psfee = parseFloat(services.psfee).toFixed(2);
        }
        orderDtl.amount = parseFloat(orderDtl.amount).toFixed(2);
        services.item.forEach((item, index) => {
          var servicesArr = [];
          if (item.services && item.services.length > 0) {
            item.services.forEach((val, idx) => {
              servicesArr.push(val.split(','));
            })
            item.servicesArr = servicesArr;
            item.subtotalPrice = 0;
            item.servicesArr.forEach((o, i) => {
              item.subtotalPrice += o[2] * o[3];
              o.push(parseFloat(o[2] * o[3]).toFixed(2))
            })
            if (orderDtl.storetype == 1 || orderDtl.storetype == 2 || orderDtl.storetype == 8 || orderDtl.storetype == 9) {

              if (!!item.amountlist) {
                if (item.amountlist.ways) {
                  item.subtotalPrice += parseFloat(item.amountlist.ways) + parseFloat(item.amountlist.specifications)
                }else{
                  item.subtotalPrice += parseFloat(item.amountlist.specifications)
                }
              }
            }
            if(orderDtl.storetype == 3){
              if (!!item.amountlist) {
                this._distance = item.amountlist.distance[0] || item.amountlist.distance;
                this._jiashou = item.amountlist.jiashou;
                this._handling = item.amountlist.handling;
                item.subtotalPrice += (parseFloat(item.amountlist.distance[0]) || parseFloat(item.amountlist.distance)) + parseFloat(item.amountlist.handling) + parseFloat(item.amountlist.jiashou);
              }
            }
          }
          item.subtotalPrice = parseFloat(item.subtotalPrice).toFixed(2);
        })
        this.setData({
          orderDtl,
          services,
          storetype: orderDtl.storetype,
          loading: false,
          handling: parseFloat(this._handling).toFixed(2),
          distance: parseFloat(this._distance).toFixed(2),
          jiashou: parseFloat(this._jiashou).toFixed(2)
        })
        isLoadcb && isLoadcb();
      }
    })
  },
  /**
   * 重新支付
   */
  _payAgain() {
    var paramsUrl = '/public/api/orders/payOrder';
    var orderId = this.data.orderId;
    var orderNum = this.data.orderDtl.ordernum;
    var payPrice = this.data.orderDtl.amount;
    var paramsData = {
      oid: orderId,
      uid: wx.getStorageSync('uid'),
      paytype: 'wxpay'
    }
    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res) => {
        util.tips('支付成功', () => {
          wx.redirectTo({
            url: '/pages/payResult/index?orderId=' + orderId + '&payPrice=' + payPrice + '&orderNum=' + orderNum,
          })
        })
      }
    })
  },
  _ringUp(evt){
    // const phoneNumber = evt.currentTarget.dataset.phone;
    // console.log(phoneNumber);
    // wx.makePhoneCall({
    //   phoneNumber
    // })
  }
}));